﻿Imports Microsoft.VisualBasic
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports System.Collections.Generic
Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling
Imports System.Data.Common

Public Class DbStoreFactory
    Private Shared connectionStringName_ As String

    Public Shared ReadOnly Property CreateDatabase As Database
        Get
            Dim result As Database = DatabaseFactory.CreateDatabase
            Try
                connectionStringName_ = GetDBServerName
                result = DatabaseFactory.CreateDatabase(connectionStringName_)
            Catch ex As Exception
                Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Configuration Policy")
                If (rethrow) Then
                    Throw
                End If
            End Try

            Return result
        End Get
    End Property
    Public Shared ReadOnly Property GetDBName() As String
        Get
            Dim db As Database = DatabaseFactory.CreateDatabase
            Dim builder As DbConnectionStringBuilder = New DbConnectionStringBuilder
            builder.ConnectionString = db.CreateConnection.ConnectionString

            Return builder("Initial Catalog").ToString
        End Get
    End Property
    Public Shared ReadOnly Property GetDBUsername() As String
        Get
            Dim db As Database = DatabaseFactory.CreateDatabase
            Dim builder As DbConnectionStringBuilder = New DbConnectionStringBuilder
            builder.ConnectionString = db.CreateConnection.ConnectionString

            Return builder("User ID").ToString
        End Get
    End Property
    Public Shared ReadOnly Property GetDBServerName() As String
        Get
            Dim db As Database = DatabaseFactory.CreateDatabase
            Dim builder As New DbConnectionStringBuilder
            builder.ConnectionString = db.CreateConnection.ConnectionString

            Return builder("Data Source").ToString
        End Get
    End Property
    Public Shared ReadOnly Property GetDBPassword() As String
        Get
            Dim db As Database = DatabaseFactory.CreateDatabase
            Dim builder As DbConnectionStringBuilder = New DbConnectionStringBuilder
            builder.ConnectionString = db.CreateConnection.ConnectionString
            If (builder.ContainsKey("password")) Then
                Return builder("Password").ToString
            Else
                Return String.Empty
            End If
        End Get
    End Property
End Class
